package com.abigdreamer.dicux.analy;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import javax.imageio.ImageIO;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.abigdreamer.newspider.HtmlSpider;
import com.abigdreamer.newspider.ThreadTaskProcesser;
import com.abigdreamer.util.FileUtil;

/**
 * 
 * @author Darkness
 * @date 2013-6-2 下午05:25:21
 * @website http://www.abigdreamer.com
 * @version V1.0
 */
public class ArticleAnalyProcessor extends ThreadTaskProcesser<ArticleAnaly> {

	HtmlSpider htmlSpider = new HtmlSpider();

	public ArticleAnalyProcessor(ArticleAnaly threadTaskManager) {
		super(threadTaskManager);
	}

	@Override
	protected void doTask(Object tmp) {
		File file = (File) tmp;
		
		String website = "dixcus";
		String html = FileUtil.readText(file);
		Document doc = Jsoup.parseBodyFragment(html);
		Elements es = doc.select("div[id=postlist]");
		es = es.select("div");
		if(es.size() > 0) {
			
			String path = "F:/dreamer/gather/" + website;// Thread.currentThread().getContextClassLoader().getResource("").getPath();
			path += "/images/" + file.getName().replace(".html", "");
			FileUtil.mkdir(path);
			
			Element el = es.get(0);
			Elements contentEl = el.select("div[class=pcb]");
			Elements imgs = contentEl.select("img");
			for (Element element : imgs) {
				String imageSrc = element.attr("src");
				//System.out.println("imageSrc:" +imageSrc);
				if(!imageSrc.startsWith("http")) {
					imageSrc = "http://hkbici.com/" + element.attr("zoomfile");
					//continue;
				}
				String[] imgInfos = imageSrc.split("/");
//				try {
//					InputStream is = htmlSpider.getInputStream(imageSrc);
					file_put_contents(path + "/" + imgInfos[imgInfos.length-1], imageSrc);
//				} catch (IOException e) {
//					e.printStackTrace();
//				}
			}
		}
		
		if (es.size() == 1) {
			getThreadTaskManager().matchCount++;
		}
	}

	public void file_put_contents(String file_name, String urlstr) {

		if(urlstr == null){
			return ;
		}
		HttpGet get = new HttpGet(urlstr);
		String[] strs = urlstr.split("/");
		String fileName = strs[strs.length-1];
	    String savePath = file_name;//IMGPATH+File.separator+fileName;
		HttpEntity entity = null;
		try {
			HttpResponse response = htmlSpider.getclient().execute(get);
			entity = response.getEntity();
			//System.out.println("保存图片>>>>.>>>>>>"+fileName);
			InputStream is = entity.getContent();
			OutputStream os = new FileOutputStream(savePath);
			IOUtils.copy(is, os);
			IOUtils.closeQuietly(os);
			IOUtils.closeQuietly(is);
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("图片保存失败");
			return ;
		}
		
//		File file = new File(file_name);
//		OutputStream os = null;
//		try {
//			os = new FileOutputStream(file);
//			byte buffer[] = new byte[4 * 1024];
//			while ((is.read(buffer)) != -1) {
//				os.write(buffer);
//			}
//			os.flush();
//		} catch (Exception e) {
//			e.printStackTrace();
//		} finally {
//			try {
//				os.close();
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}
//		return file;
	}

}
